package net.wwang.config;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @Description:
 * @Author: WangWei
 * @CreateTime: 2020-07-01 15:03
 */
public class LoginFrame extends JFrame implements ActionListener {
    JButton jb1,jb2;
    JTextField jTextField;
    JPasswordField jPasswordField;
    public LoginFrame() throws HeadlessException {
            setTitle("WwangTools V1.0  登录");
        setSize(400,220);// 窗口大小
        setLocationRelativeTo(null);// 窗口居中
        String str = "/net/wwang/static/wwang.jpg";
        Toolkit kit = Toolkit.getDefaultToolkit(); // 定义工具包
        Image myimage = kit.getImage(this.getClass().getResource(str)); //由tool获取图像
        setIconImage(myimage);
        setDefaultCloseOperation(EXIT_ON_CLOSE);// 关闭窗口则退出虚拟机
        setLayout(new FlowLayout());// 设置布局流式布局
        JPanel jPanel = new JPanel(new GridLayout(4,1));
        JPanel jPanel1 = new JPanel();
        JLabel label1 = new JLabel("账号");
        jTextField = new JTextField(12);
        jPanel1.add(label1);
        jPanel1.add(jTextField);
        jPanel.add(jPanel1);

        JPanel jPanel2 = new JPanel();
        JLabel label2 = new JLabel("密码");
        jPanel2.add(label2);
        jPasswordField = new JPasswordField(12);
        jPanel2.add(jPasswordField);
        jPanel.add(jPanel2);
        JPanel jPanel3 = new JPanel();
        jb1 = new JButton("登录");
        jb1.addActionListener(this);
        jb2 = new JButton("重置");
        jb2.addActionListener(this);
        jPanel3.add(jb1);
        jPanel3.add(jb2);
        jPanel.add(jPanel3);

        JPanel jPanel4 = new JPanel();
        JLabel label3 = new JLabel("登陆后进入系统 ---作者：CodeWwang");
        label3.setForeground(Color.DARK_GRAY);
        jPanel4.add(label3);
        jPanel.add(jPanel4);
        add(jPanel);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
    String cmd = e.getActionCommand();
    if(cmd.equals("登录")){
        String username = jTextField.getText();
        String password = String.valueOf(jPasswordField.getPassword());
        if(username.equals("admin")&&password.equals("123456")){
            setVisible(false);
           new WwangUtilsHomeFrame().showFrame();
           dispose();
        }else {
            JOptionPane.showMessageDialog(this,"用户名或者密码错误.","通知",
                    JOptionPane.ERROR_MESSAGE);
            clearText();
        }
        }else if(cmd.equals("重置")){
        clearText();
    }
    }

    private void clearText() {
        jTextField.setText("");
        jPasswordField.setText("");
    }

}
